package com.linkdood.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkdood.app.domain.Authority;
import com.linkdood.app.mapper.AuthorityMapper;
import com.linkdood.app.service.AuthorityService;
import com.linkdood.app.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 用户和菜单权限关系
 *
 * @author hycao
 */
@Slf4j
@Service
public class AuthorityServiceImpl extends ServiceImpl<AuthorityMapper, Authority> implements AuthorityService {


    /**
     * 通过 accountId 获取菜单权限
     *
     * @param accountId 账号
     * @return 菜单权限
     */
    @Override
    public Optional<Authority> getByAccountId(String accountId) {
        return lambdaQuery().eq(Authority::getAccountId, accountId).oneOpt();
    }

    /**
     * 通过 roleId 获取菜单权限
     *
     * @param roleId 账号
     * @return 菜单权限
     */
    @Override
    public Optional<Authority> findByRoleId(String roleId) {
        return lambdaQuery().eq(Authority::getCustomRoleId, roleId).oneOpt();
    }

    /**
     * 根据用户id删除  用户角色关联表
     */
    @Override
    public void deleteAccountRole(String accountId) {
        Authority one = lambdaQuery().eq(Authority::getAccountId, accountId).one();
        if (StringUtils.isNotNull(one)) {
            this.removeById(one.getId());
        }
    }

    @Override
    public Integer countByRoleId(String id) {
        return lambdaQuery().eq(Authority::getCustomRoleId, id).count();
    }
}
